package tests;

import generators.TestSuiteGenerator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class GenerateTests {
	public static void main(String[] args) throws IOException {
		File dir = new File("csv");
		for(File f : dir.listFiles()) if (f.getName().endsWith(".csv")){
			String[] parts = f.getName().split("\\.");
			String suiteName = parts[1];
			String package_sufix = parts[0];
			String java_filename = "src/tests/generated/"+package_sufix+"/"+suiteName+".java";
			
			processFile(dir.getName()+"/"+f.getName(),suiteName,package_sufix,java_filename);
		}
		
		
	}
	
	
	public static void processFile(String csv_filename,String suiteName,String package_sufix,String java_filename) throws IOException{
		System.out.println("processing "+csv_filename+"\n to -> "+java_filename);
		TestSuiteGenerator generator = new TestSuiteGenerator();
		
		BufferedReader br = new BufferedReader(new FileReader(csv_filename));
		ArrayList<String> inputs = new ArrayList<String>();
		
		ArrayList<String> outputs = new ArrayList<String>();
		
		String line = br.readLine();
		String[] columns = line.split(";");
		System.out.println(line+columns.length);
		while((line = br.readLine())!=null){
			HashMap<String, String> lineMap = new HashMap<String,String>();
			String[] values = line.split(";");
			System.out.println(line+"-"+values.length+"-"+Arrays.asList(values));
				for(int i=0;i<values.length;i++){
					lineMap.put(columns[i], values[i]);
				}
				
				outputs.add(lineMap.get("Output"));
				inputs.add(
						filter(lineMap.get("EllipseType"))+
						filter(lineMap.get("Triangle Type"))+
						filter(lineMap.get("Quadrilateral Type"))+
						filter(lineMap.get("LineType"))+
						","+
						lineMap.get("Size")+
						","+
						lineMap.get("Even")+
						","+
							toParameters(
								filter(lineMap.get("Length"))+
								filter(lineMap.get("Triangle Values"))+","+
								filter(lineMap.get("Quad Values"))+","+
								filter(lineMap.get("Side 1 Values"))+","+
								filter(lineMap.get("Side 2 Values"))+","+
								filter(lineMap.get("Side 3 Values"))+","+
								filter(lineMap.get("Side 4 Values"))+","+
								filter(lineMap.get("Minor Axis"))+","+
								filter(lineMap.get("Major Axis"))
							)
						);
		}
		
		Object[] args2 = new Object[]{suiteName,inputs, outputs,package_sufix};
		String code = generator.generate(args2);
		File f = new File(java_filename);
		f.getParentFile().mkdirs();
		FileWriter fw = new FileWriter(f);
		fw.write(code);
		fw.close();
	}
	
	private static String toParameters(String string) {
		System.out.println(string);
		StringBuffer ret = new StringBuffer();
		String[] split = string.split("[-|,]");
		for(String s: split){
			if (s.length()>0){
				ret.append(s);
				ret.append(",");
			}
		}
		if (ret.length()>1)
			return ret.deleteCharAt(ret.length()-1).toString();
		else
			return "";
	}

	private static String filter(String st){
		if (st==null)
			return "";
		return
			st;
	}

}
